package com.example.secondhandmall.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.secondhandmall.R;
import com.example.secondhandmall.activity.CollectedActivity;
import com.example.secondhandmall.activity.LoginActivity;
import com.example.secondhandmall.activity.ModifyInfoActivity;
import com.example.secondhandmall.activity.OrderActivity;
import com.example.secondhandmall.activity.PutedActivity;
import com.example.secondhandmall.api.Api;
import com.example.secondhandmall.api.ApiConfig;
import com.example.secondhandmall.api.MyCallback;
import com.example.secondhandmall.entity.UserEntity;
import com.example.secondhandmall.entity.UserInfoResponse;
import com.example.secondhandmall.view.CircleTransform;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MineFragment extends BaseFragment {

    private ImageView ivAvatar;
    private TextView tvName;
    private TextView tvSign;
    private RelativeLayout order;
    private RelativeLayout collected;
    private RelativeLayout puted;
    private Button btnLogout;
    private String avatarurl;
//    private int id;
    private String id;//到时候传参都是String
    private String account;//到时候传参都是String
    private UserEntity userEntity;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
//            goodsAdapter.setDatas(datas);
//            goodsAdapter.notifyDataSetChanged();
            switch (msg.what) {
                case 0:
                    Picasso.with(getActivity())
                            .load(userEntity.getUserPhoto())
                            .transform(new CircleTransform())
                            .into(ivAvatar);
                    avatarurl = userEntity.getUserPhoto();
                    id = ""+userEntity.getId();
                    account=userEntity.getUserId();
                    tvName.setText(userEntity.getUserName());
                    tvSign.setText(userEntity.getUserSignature());
                    break;
            }
        }
    };

    public MineFragment() {
    }

    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        ivAvatar = mRootView.findViewById(R.id.iv_avatar);
        tvName = mRootView.findViewById(R.id.tv_name);
        tvSign = mRootView.findViewById(R.id.tv_sign);
        order = mRootView.findViewById(R.id.order);
        collected = mRootView.findViewById(R.id.collected);
        puted = mRootView.findViewById(R.id.puted);
        btnLogout = mRootView.findViewById(R.id.btn_logout);
    }

    @Override
    protected void initData() {
        initInfo();
//        localInfo();
        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(OrderActivity.class);
            }
        });
        collected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(CollectedActivity.class);
            }
        });
        puted.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(PutedActivity.class);
            }
        });
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeFromSp("account");
                navigateTo(LoginActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
            }
        });
        ivAvatar.setOnClickListener(modifyClickListener);
        tvName.setOnClickListener(modifyClickListener);
        tvSign.setOnClickListener(modifyClickListener);
    }

    //接口获取用户信息
    private void initInfo() {
        HashMap<String,Object> params = new HashMap<>();
        params.put("userId",getStringFromSp("account"));
        Api.config(ApiConfig.USER_INFO,params).getRequest(new MyCallback() {
            @Override
            public void onSuccess(String res) {
                UserInfoResponse userInfoResponse = new Gson().fromJson(res,UserInfoResponse.class);
                if(userInfoResponse.getCode()==200){
                    userEntity = userInfoResponse.getData();
                    mHandler.sendEmptyMessage(0);
//                    Picasso.with(getActivity())
//                            .load(userEntity.getUserPhoto())
//                            .transform(new CircleTransform())
//                            .into(ivAvatar);
//                    avatarurl = userEntity.getUserPhoto();
//                    id = ""+userEntity.getId();
//                    account=userEntity.getUserId();
//                    tvName.setText(userEntity.getUserName());
//                    tvSign.setText(userEntity.getUserSignature());
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure",e.toString());
            }
        });
    }

    //本地模拟用户信息
    private void localInfo(){
        UserEntity userEntity = new UserEntity();
        userEntity.setId(1);
        userEntity.setUserId("zx");
        userEntity.setUserPhoto("https://img.alicdn.com/bao/uploaded/i2/373559220/O1CN01EGoCAx2HyoKWG8qK6_!!373559220.jpg_200x200q90.jpg_.webp");
        userEntity.setUserName("Darius");
        userEntity.setUserSignature("二手商城。。。。。。");
        Picasso.with(getActivity())
                .load(userEntity.getUserPhoto())
                .transform(new CircleTransform())
                .into(ivAvatar);
        tvName.setText(userEntity.getUserName());
        tvSign.setText(userEntity.getUserSignature());
        avatarurl = userEntity.getUserPhoto();
        id = ""+userEntity.getId();
        account=userEntity.getUserId();
    }

    View.OnClickListener modifyClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//            HashMap<String,Object> info = new HashMap<>();
//            info.put("name",tvName.getText());
//            info.put("sign",tvSign.getText());
//            info.put("avatar",avatarurl);
//            info.put("id",id);
            List<String> info =new ArrayList<>();
            info.add(tvName.getText().toString());
            info.add(tvSign.getText().toString());
            info.add(avatarurl);
            info.add(id);
            info.add(account);
            navigateToForResult(ModifyInfoActivity.class,"info", (Serializable) info,1);
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1){
            initInfo();
        }
    }
}